<template>
  <div>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title  am-cf">招生提成</div>
        </div>
        <div class="widget-body  am-fr">
          <div class="am-u-sm-12 am-form ">
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" name="teacherName" v-model="query.teacherName" placeholder="请输入教师姓名"/>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-9">
              <div class="am-form-group">
                <input type="text" @click="handleRange" v-model="query.periodTypeQuery" placeholder="请选择筛选范围"/>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-u-end">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="search" ><span class="am-icon-search"></span>查询
                </button>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
              <div class="am-form-group am-btn-group-xs">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="exportData" v-if="hasPermission('export')">导出下载
                </button>
              </div>
            </div>
          </div>

          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%">
              <el-table-column
                prop="teacherName"
                label="姓名"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="busTeamName"
                label="人事单位"
                min-width="200">
              </el-table-column>
              <el-table-column
                prop="newOldStudentNum"
                label="新老生科数"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="newOldStudentAmount"
                label="新老生奖金"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="seqStudentNum"
                label="续读生顺期科数"
                min-width="140">
              </el-table-column>
              <el-table-column
                prop="stepStudentNum"
                label="续读生跨期科数"
                min-width="140">
              </el-table-column>
              <el-table-column
                prop="seqStepStudentNum"
                label="续读生总科数"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="seqStepStudentAmount"
                label="续读生奖金"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="totalAmount"
                label="总奖金"
                min-width="100">
              </el-table-column>
            </el-table>
          </div>
          <teacher-commission-detail @completed="handleCompleted" ref="teacherCommissionDetail"></teacher-commission-detail>
          <div class="am-u-lg-12 am-cf">
            <div class="am-fr">
              <pagination v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize"
                          @paging="loadTableData"/>
            </div>
          </div>

        </div>
      </div>
    </div>

  </div>
</template>
<script>
  import io from '../../lib/io'
  import moment from 'moment'
  import Pagination from '../base/Pagination'
  import TeacherCommissionDetail from './TeacherCommissionDetail.vue'

  export default{
    data: function () {
      return {
        tableData: [],
        total: 0,
        pageSize: 10,
        pageNo: 1,
        periodTypeQueryVo: [],
        query: {
          teacherName: '',
          periodTypeQuery: '',
        },
      }
    },
    components: {
      Pagination, TeacherCommissionDetail
    },
    mounted: function () {
      $(window).smoothScroll()
    },
    created: function () {
      this.loadTableData()
    },
    computed: {
    },
    watch:{
    },
    methods: {
      search: function () {
        this.loadTableData(1)
      },
      handleCompleted (result) {
        let len = 0
        this.periodTypeQueryVo = []
        result.value.map(val => {
          let timeTypeQueryVoList = []
          if (val.periodId && val.checkList.length > 0) {
            val.checkList.map(i => {
              if (val.timeTypeList[Number(i)].time.length === 2) {
                len++
                timeTypeQueryVoList.push({
                  type: i,
                  startDate: moment(val.timeTypeList[Number(i)].time[0]).format('YYYY-MM-DD'),
                  endDate: moment(val.timeTypeList[Number(i)].time[1]).format('YYYY-MM-DD'),
                })
              }
            })
            if (timeTypeQueryVoList.length > 0) {
              this.periodTypeQueryVo.push({
                periodId: val.periodId,
                timeTypeQueryVoList: timeTypeQueryVoList
              })
            }
          }
        })
        this.query.periodTypeQuery = '已选择' + result.areaTeamName + ' ' + len + ' 条数据'
        this.loadTableData(1)
      },
      handleRange () {
        this.$refs.teacherCommissionDetail.show()
      },
      loadTableData: function (pageNo) {
        var _this = this
        _this.pageNo = pageNo || _this.pageNo || 1
        if (this.periodTypeQueryVo.length === 0 ) {
          return
        }
        _this.$showLoading()
        io.post(io.findTeacherCommission, $.extend({
          pageNo: _this.pageNo,
          pageSize: _this.pageSize,
          periodTypeQueryVoJson: JSON.stringify(this.periodTypeQueryVo),
        }, _this.query), function (ret) {
          _this.$hiddenLoading()
          if (ret.success) {
            _this.total = ret.data.total
            _this.tableData = ret.data.list
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      exportData:function(){
        if (this.periodTypeQueryVo.length === 0 ) {
          this.$alert('请先选择筛选范围！')
          return
        }
        io.downloadFile(io.exportTeacherCommission , {
          teacherName: this.query.teacherName,
          periodTypeQueryVoJson: JSON.stringify(this.periodTypeQueryVo),
        } )
      },
    }
  }
</script>
<style scoped>
  .red {
    color: #ea3a3a;
  }
</style>
